/**
 * author:Administrator
 * date:2013-5-21
 */
package com.roc.web.action.setting;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction; // import com.roc.pojo.Merchant;
import com.roc.pojo.Operators;
import com.roc.pojo.SettingBean; // import com.roc.pojo.ProfitRule;
import com.roc.service.IPagenitionService; // import
import com.roc.service.IorgService;
import com.roc.service.ServiceException;
import com.roc.service.setting.ISettflagService;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.web.action.MerchantSearchCondition;

/**
 * @author Feng haitao
 * 
 */
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/hrtsetting")
public class HRTSettingAction extends PaginationBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5236170890634487270L;
	private Logger logger = LogManager.getLogger(HRTSettingAction.class);
	private Operators operators;
	private List<Object> profitRsList = new ArrayList<Object>();
	private HRTSettingSearchCondition searchCondition;

	private String msg;

	@Autowired
	IPagenitionService pagenitionService;

	@Autowired
	IorgService orgService;
	@Autowired
	ISettflagService settflagService;

	// 结算查询
	@Action(value = "hrtsettingsearch", results = {
			@Result(name = "success", location = "/pages/setting/hrtsettinglist.jsp"),
			@Result(name = "error", location = "/pages/setting/hrtsettinglist.jsp"),
			@Result(name = "login", location = "/pages/redirect.jsp") })
	public String search() {
		Operators logingMerchant = (Operators) getSession().getAttribute(
				Constant.SESSION_LOGIN_MERCHANT);
		Map<String, Operators> merchantMap = new TreeMap<String, Operators>();
		if (searchCondition == null) {
			searchCondition = (HRTSettingSearchCondition) getSearchConditionFromSession();
			if (searchCondition == null) {
				searchCondition = new HRTSettingSearchCondition();
			}
		}
		try {
			// 系统管理员查看全部，代理只能看到本机构下的。
//			if (Constance.MERCHANT_TYPE_DAILI.equals(logingMerchant
//					.getRoleType())) {
//				searchCondition.setOrgCode(logingMerchant.getOrgCode());
//			}
			int rows = pagenitionService.countSQL(searchCondition);
			List<SettingBean> settingList = new ArrayList<SettingBean>();
			if (rows > 0) {
				searchRsList = pagenitionService.findAllSQL(searchCondition,
						calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
				if (searchRsList != null && searchRsList.size() > 0) {
					Iterator iterator = searchRsList.iterator();
					while (iterator.hasNext()) {
						Object[] obj = (Object[]) iterator.next();
						SettingBean sett = new SettingBean();
						if (obj[0] != null && !"".equals(obj[0])) {
							sett.setOrderno(obj[0].toString());
						}
						if (obj[1] != null && !"".equals(obj[1])) {
							sett.setMerchantno(obj[1].toString());
						}
						if (obj[2] != null && !"".equals(obj[2])) {
							sett.setMerchantname(obj[2].toString());
						}
						if (obj[3] != null && !"".equals(obj[3])) {
							sett.setSettledate(obj[3].toString());
						}
						if (obj[4] != null && !"".equals(obj[4])) {
							sett.setFreezeflag(obj[4].toString());
						}
						if (obj[5] != null && !"".equals(obj[5])) {
							sett.setTotalamt(Double.valueOf(obj[5].toString()));
						}
						if (obj[6] != null && !"".equals(obj[6])) {
							sett
									.setTotalnum(Integer.valueOf(obj[6]
											.toString()));
						}
						if (obj[7] != null && !"".equals(obj[7])) {
							sett.setMerchantfee(Double.valueOf(obj[7]
									.toString()));
						}
						if (obj[8] != null && !"".equals(obj[8])) {
							sett.setMerchantsettleamt(Double.valueOf(obj[8]
									.toString()));
						}
						if (obj[9] != null && !"".equals(obj[9])) {
							sett.setSettleflag(obj[9].toString());
						}
						if (obj[10] != null && !"".equals(obj[10])) {
							sett.setBatchno(obj[10].toString());
						}
						if (obj[11] != null && !"".equals(obj[11])) {
							sett.setSetdate(obj[11].toString());
						}
						if (obj[12] != null && !"".equals(obj[12])) {
							sett.setBankdeposit(obj[12].toString());
						}
						if (obj[13] != null && !"".equals(obj[13])) {
							sett.setAccountname(obj[13].toString());
						}
						if (obj[14] != null && !"".equals(obj[14])) {
							sett.setAccountno(obj[14].toString());
						}
						settingList.add(sett);
					}
				}
			}
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute("settingList", settingList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);

		} catch (ServiceException e) {

			logger.error(e.getMessage(), e);

		} catch (Exception e) {
			logger.error(e.getMessage(), e);

		}
		return SUCCESS;
	}

	// 结算初始页面
	@Action(value = "hrtsettinglist", results = {
			@Result(name = "success", location = "/pages/setting/hrtsettinglist.jsp"),
			@Result(name = "login", location = "/pages/redirect.jsp"),
			@Result(name = "error", location = "/error.jsp") }

	)
	public String settinglist() throws Exception {
		// List settingflagList=new ArrayList();
		// settingflagList=settflagService.querySettingFlagList();
		// getRequest().setAttribute("settingflagList", settingflagList);
		return SUCCESS;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSession()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (HRTSettingSearchCondition) getSession().getAttribute(
				SEARCH_SESSION_BEAN_HRTSETTING);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSubClass()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#setSearchConditionToSubClass(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#setSearchToConditionSession(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_BEAN_HRTSETTING, searchCondition);
	}

	/**
	 * @return the msg
	 */
	public String getMsg() {
		return msg;
	}

	/**
	 * @param msg
	 *            the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * @return the profitRsList
	 */
	public List<Object> getProfitRsList() {
		return profitRsList;
	}

	/**
	 * @param profitRsList
	 *            the profitRsList to set
	 */
	public void setProfitRsList(List<Object> profitRsList) {
		this.profitRsList = profitRsList;
	}

	/**
	 * @return the profitModelHead
	 */

	public Operators getOperators() {
		return operators;
	}

	public void setOperators(Operators operators) {
		this.operators = operators;
	}

	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return null;
	}

	public HRTSettingSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(HRTSettingSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}



}
